<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../header.jsp"%>

<div class="left ">
<div class="border bg">
<div class="page-header">
<h2>新建通知</h2>
<div class="cms_toolbar">
	<a href="javascript:;" onclick="goBack('./list.do')" class="btn"><i class="icon-list"></i>返回</a>
</div>
</div>

<div>
<form id="detailForm" class="form-horizontal">
<fieldset>
	<div class="control-group">
		<label for="name"  class="control-label">标题：</label>
		<div class="controls">
		<input type="text" id="name" name="name" class="input-xxlarge" maxlength="50" /> <span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label for="content"  class="control-label">内容：</label>
		<div class="controls">
		<textarea id="content" name="content" class="input-xxlarge" maxlength="4000"></textarea>
		<span class="cms_color_required">*</span>
		</div>
	</div>
	<div class="control-group">
		<label class="control-label">接收人员：</label>
		<div class="controls">
			<div id="noticeUserTreeDiv"></div>
		</div>
	</div>
	<div class="form-actions">
		<a id="saveBtn" href="javascript:void(0);" class="btn btn-primary" onclick="doSave()"><i class="icon-ok"></i>发布</a>
	</div>
</fieldset>
</form>
</div>

</div>
</div>

<script type="text/javascript">
function doSave() {
	if (!detailValidator.form()) {
		return;
	}
	if(organs.length == 0 && users.length == 0) {
		$.alert("请选择接收人员。");
		return;
	}
	
	block("#detailForm");
	$.ajax({
		type: "POST",
		url: "save.json",
		data: getNoticeData(),
		dataType: "json",
		success: saveSucceedCallback,
		error:function(message){
			unblock("#detailForm");
			$.alert("出错了，错误信息为：\n"+message);
		}
	});
}

function getNoticeData() {
	return {
		name : $("#name").val(),
		content : $("#content").val(),
		receivers : users,
		receiveOrgans : organs
	};
}

function saveSucceedCallback(data) {
	unblock("#detailForm");
	if (!data.succeed) {
		$.notice(data.message);
		return;
	}

	$.info("保存成功！");
	var url = "detail.do?id=" + data.data;
	location.href = url;
}

var organs = "", users = "";
function getTreeChecked() {
	organs = "";
	users = "";
	var roots = jQuery.jstree._reference("#noticeUserTreeDiv")._get_children(-1);
	for(var i = 0; i < roots.length; i++) {
		handleNode(roots[i]);
	}
}

function handleNode(node) {
	var id = $(node).data("id");
	var type = $(node).data("type");
	var checked = jQuery.jstree._reference("#noticeUserTreeDiv").is_checked(node);
	if(type == "user") {
		if(checked){
			if(users == "") {
				users = id;
			} else {
				users = users + "," + id;
			}
		}
		return false;
	}
	
	// 机构 
	if(checked) {
		if(organs == "") {
			organs = id;
		} else {
			organs = organs + "," + id;
		}
		return false;
	}
	
	var children = jQuery.jstree._reference("#noticeUserTreeDiv")._get_children(node);
	if(!children){
		return false;
	}
	for(var i = 0; i < children.length; i++) {
		handleNode(children[i]);
	}
}

var _tree;
$("#noticeUserTreeDiv").ready(function() {
	_tree = $.readonlyTree("#noticeUserTreeDiv",
			ctx + "/biz/system/user/tree.json",
			{
				two_state:false
			});

	_tree.bind("change_state.jstree",function(e,data) {
		getTreeChecked();
	});
});

var detailValidator;
$(document).ready(function() {
	detailValidator = $("#detailForm").validate({
		rules: {
			name:{required:true, maxlength:50 },
			content:{required:true, maxlength:4000}
		}
	});
	$("textarea").tah({
		moreSpace:10
	});
	$().maxlength();
});


</script>


<%@ include file="../../footer.jsp"%>